﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;
using System.Data.SqlClient;
using SubSonic;
using System.IO;
public partial class User_Shares_VillageList1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
        
        //SqlDataSource1.ConnectionString = DataAccess.Global.GetConnectionString();
    }
    //protected void DropDownVillage_SelectedIndexChanged(object sender, EventArgs e)
    //{

    //}
    //protected void DropDownCity_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    //SubSonic.SqlQuery q = new SubSonic.Select().From(TblBakhsh.Schema).From(TblCity.Schema).From(TblDehestan.Schema).From(TblVillage.Schema).InnerJoin( TblVillage.Schema.Column.Columns.DehstanId,TblDehestan.Columns.Id).InnerJoin("TblCity", TblCity.Columns.Id, "TblBakhsh", TblBakhsh.Columns.CityId);
    //    //GridView1.DataSource= q.ExecuteReader();
    //    //GridView1.DataBind();
    //}
    public void cleargrid()
    {
      
        GridView1.DataSource = "";
        GridView1.DataBind();
    }
    protected void DropDownCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        cleargrid();
        TblBakhshCollection bakhshCollection = new TblBakhshCollection();
        TblDehestanCollection dehestanCollection = new TblDehestanCollection();
        TblVillageCollection villageCollection = new TblVillageCollection();
        bakhshCollection.LoadAndCloseReader(TblBakhsh.FetchByParameter(TblBakhsh.Columns.CityId, Comparison.Equals,
                                                                       DropDownCity.SelectedValue));
        foreach (var baksh in bakhshCollection)
        {
            dehestanCollection.LoadAndCloseReader(TblDehestan.FetchByParameter(TblDehestan.Columns.BakhshId,
                                                                               Comparison.Equals, baksh.Id));
            foreach (var dehestan in dehestanCollection)
            {
                villageCollection.LoadAndCloseReader(TblVillage.FetchByParameter(TblVillage.Columns.DehstanId,
                                                                                 Comparison.Equals, dehestan.Id));
            }
            dehestanCollection.Clear();

        }
        bakhshCollection.Clear();
        GridView1.DataSource = villageCollection.ToList();
        GridView1.DataBind();
    }

    protected void DropDownBakhsh_SelectedIndexChanged(object sender, EventArgs e)
    {
        cleargrid();
        List<TblVillage> output = new List<TblVillage>();
        TblBakhshCollection bakhshCollection = new TblBakhshCollection();
        TblDehestanCollection dehestanCollection = new TblDehestanCollection();
        TblVillageCollection villageCollection = new TblVillageCollection();

        dehestanCollection.LoadAndCloseReader(TblDehestan.FetchByParameter(TblDehestan.Columns.BakhshId,
                                                                           Comparison.Equals, DropDownBakhsh.SelectedValue));
        foreach (var dehestan in dehestanCollection)
        {
            villageCollection.LoadAndCloseReader(TblVillage.FetchByParameter(TblVillage.Columns.DehstanId,
                                                                             Comparison.Equals, dehestan.Id));
        }
        dehestanCollection.Clear();


        foreach (var village in villageCollection)
        {
            output.Add(village);
        }
        villageCollection.Clear();
        GridView1.DataSource = output;
        GridView1.DataBind();
        output.Clear();
  
    }
    protected void DropDownDehestan_SelectedIndexChanged(object sender, EventArgs e)
    {
        //cleargrid();
        //List<TblVillage> output = new List<TblVillage>();
        //TblBakhshCollection bakhshCollection = new TblBakhshCollection();
        //TblDehestanCollection dehestanCollection = new TblDehestanCollection();
        //TblVillageCollection villageCollection = new TblVillageCollection();
        //bakhshCollection.LoadAndCloseReader(TblBakhsh.FetchByParameter(TblBakhsh.Columns.CityId, Comparison.Equals,
        //                                                               DropDownCity.SelectedValue));
        //foreach (var baksh in bakhshCollection)
        //{
        //    dehestanCollection.LoadAndCloseReader(TblDehestan.FetchByParameter(TblDehestan.Columns.BakhshId,
        //                                                                       Comparison.Equals, DropDownBakhsh.SelectedValue));
        //    foreach (var dehestan in dehestanCollection)
        //    {
        //        villageCollection.LoadAndCloseReader(TblVillage.FetchByParameter(TblVillage.Columns.DehstanId,
        //                                                                         Comparison.Equals,DropDownDehestan.SelectedValue));
        //    }

        //    dehestanCollection.Clear();
        //} 

        //foreach (var village in villageCollection)
        //{
        //    output.Add(village);
        //}
        //GridView1.DataSource = output;
        //GridView1.DataBind();
        //output.Clear();
        cleargrid();
        GridView1.DataSource = DataAccess.TblVillage.FetchByParameter(DataAccess.TblVillage.Columns.DehstanId, DropDownDehestan.SelectedValue);
        GridView1.DataBind();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    protected void ExportToExcel_Click(object sender, EventArgs e)
    {

        try
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.Encoding.Unicode;
            Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
            //this.EnableViewState = false;
            StringWriter sr = new StringWriter();
            HtmlTextWriter hr = new HtmlTextWriter(sr);
            GridView1.RenderControl(hr);
            Response.Write(sr.ToString());
            sr.Dispose();
            hr.Dispose();
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string p = e.Row.Cells[8].Text;
                if (p != "")
                {
                    TblDehestan t = new TblDehestan();
                    t = TblDehestan.FetchByID(p);
                    e.Row.Cells[3].Text = t.Name;
                    string bakhshid =t.BakhshId.ToString();
                    TblBakhsh b = new TblBakhsh();
                    b=TblBakhsh.FetchByID(bakhshid);
                    e.Row.Cells[2].Text = b.Name;
                    string cityid = b.CityId.ToString();
                    TblCity c = new TblCity();
                    c=TblCity.FetchByID(cityid);
                    e.Row.Cells[1].Text = c.Name;
                    e.Row.Cells[7].Visible = false;
                    e.Row.Cells[8].Visible = false;
                }
            }
        }
        catch
        {
        }
    }

    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count > 0)
        {
            //This replaces <td> with <th> and adds the scope attribute
            GridView1.UseAccessibleHeader = true;

            //This will add the <thead> and <tbody> elements
            GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;

            //This adds the <tfoot> element. 
            //Remove if you don't have a footer row
            GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
        }
    }
}